"""
@Project ：HaloTeachingPro 
@File    ：ReadYamlFileUtil.py
@IDE     ：PyCharm 
@Author  ：ceiling
@Date    ：2025/4/13 11:29 
"""
import yaml
from pathlib import Path


class ReadYamlFileUtil:

    @classmethod
    def load_yaml_data_as_tuples(cls, yaml_path, *fields):
        """
        读取 YAML 文件并转换为 pytest 参数化格式：[(...), (...)]

        :param yaml_path: YAML 文件路径
        :param fields: 想要提取的字段名（顺序决定顺序）
        :return: list[tuple]
        """
        yaml_file = Path(yaml_path)
        if not yaml_file.exists():
            raise FileNotFoundError(f"YAML 文件不存在: {yaml_path}")

        with open(yaml_file, encoding='utf-8') as f:
            data = yaml.safe_load(f)

        result = []
        # 处理列表结构
        if isinstance(data, list):
            for item in data:
                result.append(tuple(item[field] for field in fields))
        # 处理字典结构
        elif isinstance(data, dict):
            for key, item in data.items():
                result.append(tuple(item[field] for field in fields))
        else:
            raise TypeError("YAML 数据必须是 list 或 dict 格式")

        return result


